import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq } from '../type'

/**
 * 部门接口
 */
export const deptApi = {
  /**
   * 获取部门列表
   */
  getDeptList(data: IGetDeptListReq): Promise<IGetDeptListRes> {
    return ajax.post('/system/dept/list', data)
  },
  /**
   * 获取部门树
   */
  getDeptTree(): Promise<IGetDeptTreeRes> {
    return ajax.get('/system/dept/tree')
  },
  /**
   * 添加部门
   */
  createDept(data: ICreateDeptReq) {
    return ajax.post('/system/dept/create', data)
  },
  /**
   * 更新部门
   */
  updateDept(id: string, data: IUpdateDeptReq) {
    return ajax.put('/system/dept/update', data, { params: { id } })
  },
  /**
   * 删除部门
   */
  deleteDept(data: IDeleteReq) {
    return ajax.delete('system/dept/delete', { data })
  }
}

export type IGetDeptListReq = components['schemas']['FindDeptListDTO']
export type IGetDeptListRes = RequiredDeep<
  components['schemas']['FindDeptListVO']['data']
>
export type IDeptListItem = IGetDeptListRes[0]
export type IGetDeptTreeRes = RequiredDeep<
  components['schemas']['GetDeptTreeVO']['data']
>
export type ICreateDeptReq = components['schemas']['CreateDeptDTO']
export type IUpdateDeptReq = components['schemas']['UpdateDeptDTO']
